<!--
  * 入库报表-销退入库统计
  *
-->
<template>
  <!---------- 查询表单form begin ----------->
  <a-card size="small" :bordered="false" :hoverable="false" class="query-form-wrapper smart-margin-bottom10 smart-radius-2">
    <a-form>
      <a-row :gutter="[8, 10]">
        <a-col :span="4">
          <a-form-item label="商品名称" class="smart-margin-bottom0">
            <a-input v-model:value.trim="queryForm.goods_name" placeholder="商品名称" allow-clear />
          </a-form-item>
        </a-col>
        <a-col :span="3">
          <a-form-item label="经销商" class="smart-margin-bottom0">
            <DealerSelect v-model:value="queryForm.dealer_id" placeholder="经销商" @submit="onSearch" />
          </a-form-item>
        </a-col>
        <!-- <a-col :span="5">
          <a-form-item label="单据日期" class="smart-margin-bottom0">
            <a-range-picker :allowEmpty="[true, true]" v-model:value="queryForm.create_time" valueFormat="YYYY-MM-DD" allow-clear :presets="TIME_RANGE_PRESETS" @change="onSearch" />
          </a-form-item>
        </a-col> -->
        <a-col :span="5">
          <a-button-group>
            <a-button type="primary" html-type="submit" @click="onSearch" v-privilege="'goods:query'">
              <template #icon>
                <SearchOutlined />
              </template>
              查询
            </a-button>
            <a-button @click="resetQuery" v-privilege="'goods:query'">
              <template #icon>
                <ReloadOutlined />
              </template>
              重置
            </a-button>
          </a-button-group>
        </a-col>
      </a-row>
    </a-form>
  </a-card>
  <!---------- 查询表单form end ----------->

  <a-card size="small" :bordered="false" :hoverable="true" class="query-table-wrapper">
    <!---------- 表格操作行 begin ----------->
    <a-row class="smart-table-btn-block">
      <div class="smart-table-operate-block">
        <a-button @click="onExportGoods" type="primary" v-privilege="'goods:exportGoods'">
          <template #icon>
            <ExportOutlined />
          </template>
          导出
        </a-button>
      </div>
      <div class="smart-table-setting-block">
        <TableOperator v-model="columns" :tableId="TABLE_ID_CONST.BUSINESS.ERP.GOODS" :refresh="queryData" />
      </div>
    </a-row>
    <!---------- 表格操作行 end ----------->
    <div class="table-wrapper">
      <a-table size="small" :dataSource="tableData" :columns="columns" rowKey="id" bordered :scroll="{ x: 'max-content', y: tableScrollY }" :pagination="false" :showSorterTooltip="false" :loading="tableLoading" :row-selection="{}">
        <template #summary>
          <a-table-summary-row>
            <a-table-summary-cell>总计</a-table-summary-cell>
            <a-table-summary-cell />
            <a-table-summary-cell />
            <a-table-summary-cell />
            <a-table-summary-cell>
              <a-typography-text type="danger">{{ (total_amount.num_count || 0).toFixed(2) }}</a-typography-text>
            </a-table-summary-cell>
            <a-table-summary-cell>
              <a-typography-text type="danger">{{ (total_amount.small_unit_num_count || 0).toFixed(2) }}</a-typography-text>
            </a-table-summary-cell>
            <a-table-summary-cell>
              <a-typography-text type="danger">{{ Number(total_amount.value_count || 0).toFixed(2) }}</a-typography-text>
            </a-table-summary-cell>
            <a-table-summary-cell />
          </a-table-summary-row>
        </template>
      </a-table>
    </div>

    <div class="smart-query-table-page query-table-page">
      <a-pagination showSizeChanger showQuickJumper show-less-items :pageSizeOptions="PAGE_SIZE_OPTIONS" :defaultPageSize="queryForm.limit" v-model:current="queryForm.page" v-model:pageSize="queryForm.limit" :total="total" @change="queryData" @showSizeChange="queryData" :show-total="() => `共${total}条`" />
    </div>
  </a-card>
</template>
<script setup lang="ts">
import { outApi } from '@/api/business/report/warehouse-api'
import DealerSelect from '@/components/business/select/dealer-select.vue'
import TableOperator from '@/components/support/table-operator/index.vue'
import { useQuery } from '@/hooks/web/use-query-hook'
import { PAGE_SIZE_OPTIONS, DEFAULT_TIME_RANGE } from '@/constants/system/common-const'
import { TABLE_ID_CONST } from '@/constants/support/table-id-const'
import { QUERY_FORM_OP } from '@/constants/support/query-form-op-const'
import { useResize } from '@/hooks/web/use-resize-hook'

// ----------------------------- 计算表格高度 -----------------------------
const { tableScrollY } = useResize()
// ---------------------------- 表格列 ----------------------------

const columns = ref([
  {
    title: '商品名称',
    dataIndex: 'goods_name'
  },
  {
    title: '商品编码',
    dataIndex: 'code'
  },
  {
    title: '商品分类',
    dataIndex: 'goods_cate'
  },
  {
    title: '入库数量',
    dataIndex: 'num'
  },
  {
    title: '小单位数量',
    dataIndex: 'small_unit_num'
  },
  {
    title: '入库金额',
    dataIndex: 'value'
  },
  {
    title: '经销商',
    dataIndex: 'dealer_name'
  }
])

// ---------------------------- 查询数据表单和方法 ----------------------------
const total_amount = ref({
  num_count: 0,
  small_unit_num_count: 0,
  value_count: 0
})
const { queryForm, tableData, tableLoading, total, resetQuery, onSearch, queryData } = useQuery({
  fetchDataApi: async (params) => {
    const data = {
      ...params,
      type: 2
    }
    const res = await outApi.queryPutList(data)
    const { from, ...other } = res.data
    total_amount.value = {
      num_count: other.num_count,
      small_unit_num_count: other.small_unit_num_count,
      value_count: other.value_count
    }
    return {
      list: res.data.data,
      total: res.data.total
    }
  },
  queryFormFilter: {
    goods_name: null,
    dealer_id: null
    // create_time: [],
  },
  queryFormOp: {
    goods_name: QUERY_FORM_OP['模糊查询'],
    dealer_id: QUERY_FORM_OP['全等查询']
    // create_time: QUERY_FORM_OP['范围查询'],
  }
})
onMounted(() => {
  queryData()
})

// ------------------------------- 导出和导入 ---------------------------------

async function onExportGoods() {
  await outApi.exportPut({ export: 1, type: 2 })
}
</script>
